@extends('layouts.app')

@section('content')
    @include('vendor.ueditor.assets')
    <div class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="panel panel-default">
                    <!-- 编辑器容器 -->
                    <div class="panel-heading">发布问题</div>
                    <div class="panel-body">

                        <form action="/question" method="post">
                            {!! csrf_field() !!}
                            <div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}">
                                <label for="title">标题</label>
                                <input type="text" value="{{ old('title') }}" class="form-control" id="title" name="title" placeholder="标题" required>
                                @if ($errors->has('title'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('title') }}</strong>
                                    </span>
                                @endif
                            </div>
                            <div class="form-group">
                                <label for="topic">话题</label>
                                <select name="topics[]" id="topics-selecte2" class="form-control" multiple="multiple">
                                </select>
                            </div>
                            <div class="form-group{{ $errors->has('body') ? ' has-error' : '' }}">
                                <label for="body">描述</label>
                                <script id="container" name="body" type="text/plain">
                                    {!! old('body')  !!}
                                </script>
                                @if ($errors->has('body'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('body') }}</strong>
                                    </span>
                                @endif
                            </div>

                            <button class="btn btn-default pull-right">发布</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    @section('js')
    <!-- 实例化编辑器 -->
    <script type="text/javascript">
      var ue = UE.getEditor('container');
      ue.ready(function() {
        ue.execCommand('serverparam', '_token', '{{ csrf_token() }}'); // 设置 CSRF token.
      });


      $(document).ready(function() {

        function formatTopic (topic) {
          return "<div class='select2-result-repository clearfix'>" +
          "<div class='select2-result-repository__meta'>" +
          "<div class='select2-result-repository__title'>" +
          topic.name ? topic.name : "Laravel"   +
            "</div></div></div>";
        }


        function formatTopicSelection (topic) {
          return topic.name || topic.text;
        }

        jQuery("#topics-selecte2").select2({
          tags: true,
          placeholder: '选择相关话题',
          minimumInputLength: 2,
          ajax: {
            url: '/api/topics',
            dataType: 'json',
            delay: 250,
            data: function (params) {
              return {
                q: params.term
              };
            },
            processResults: function (data, params) {
              return {
                results: data
              };
            },
            cache: true
          },
          templateResult: formatTopic,
          templateSelection: formatTopicSelection,
          escapeMarkup: function (markup) {
            return markup;
          }
        });
      });


    </script>
    @endsection

@endsection


